<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>

<%@ page import="com.scalar.chiptrack.utils.ConfigManager"%>
<%String chipTracContext = "/" + ConfigManager.getChipTracContext(); %>

<HTML>
<HEAD>
<TITLE>ScalarSoft - ChipTrac</TITLE>
<LINK href="<%=chipTracContext%>/jsp/includes/stylesheets/scalarsoft.css" type="text/css" rel="stylesheet">
<SCRIPT language="JavaScript" src="<%=chipTracContext%>/jsp/includes/scripts/validations.js"></script>
<SCRIPT src="<%=chipTracContext%>/jsp/includes/scripts/functions.js"></script>

<SCRIPT language="JavaScript">


//var customerId = <%= request.getParameter("customerId")%>;
var customerId = "";

//Function for validating form data
function validateData(theForm)
{
    var pa = document.getElementById("errors");
    pa.innerHTML = "";
    var address1Obj         = document.packingSlipForm[ 'newShippingAddress.address1' ];
    var emailObj            = document.packingSlipForm[ 'newShippingAddress.email' ];
    var phoneNoObj          = document.packingSlipForm[ 'newShippingAddress.phoneNumber' ];
    var faxNoObj            = document.packingSlipForm[ 'newShippingAddress.faxNumber' ];
    var cellNoObj           = document.packingSlipForm[ 'newShippingAddress.cellNumber' ];
    var contactPersonObj    = document.packingSlipForm[ 'newShippingAddress.contactPerson' ];

    if( trim( address1Obj.value ).length == 0 )
    {
        pa.innerHTML="<li>Address1 cannot be empty";
        address1Obj.focus();
        return false;
    }
    if( (trim(phoneNoObj.value).length != 0)  || (trim(faxNoObj.value).length != 0 ) || (trim(cellNoObj.value).length != 0) || (trim(emailObj.value).length != 0) )
    {
        if(trim(contactPersonObj.value).length==0)
        {
            pa.innerHTML="<li>Contact Person cannot be empty when Phone Number or Fax Number or Cell Number or Email is Entered";
            contactPersonObj.focus();
            return false;
        }
    }
    if (!isValidMailID(emailObj.value,emailObj,"N"))
    {
        pa.innerHTML="Enter a Valid Email ID";
        emailObj.select();
        emailObj.focus();
        return false;
    }
    return true;
}

function addAddress(theForm)
{

    var contactPersonObj    = document.packingSlipForm[ 'newShippingAddress.contactPerson'];
    var address1Obj         = document.packingSlipForm[ 'newShippingAddress.address1'];
    var address2Obj         = document.packingSlipForm[ 'newShippingAddress.address2'];
    var address3Obj         = document.packingSlipForm[ 'newShippingAddress.address3'];
    var address4Obj         = document.packingSlipForm[ 'newShippingAddress.address4'];
    var cityObj             = document.packingSlipForm[ 'newShippingAddress.city'];
    var stateObj            = document.packingSlipForm[ 'newShippingAddress.state'];
    var countryObj            = document.packingSlipForm[ 'newShippingAddress.country'];
    var zipCodeObj          = document.packingSlipForm[ 'newShippingAddress.zipCode'];
    var phoneNumberObj      = document.packingSlipForm[ 'newShippingAddress.phoneNumber'];
    var cellNumberObj       = document.packingSlipForm[ 'newShippingAddress.cellNumber'];
    var faxNumberObj        = document.packingSlipForm[ 'newShippingAddress.faxNumber'];
    var emailObj            = document.packingSlipForm[ 'newShippingAddress.email'];

    //window.opener.document.salesOrderForm.shippingAddress.value=theForm.address1.value+"\n"+
    //theForm.address2.value+"\n"+theForm.city.value+"\n"+theForm.state.value+"\n"+theForm.zipCode.value+"\n"+theForm.country.value;


    if(!validateData(theForm))
        return;

    //For setting the shipping address in the shippingAddress text area  and shippingAddressID select list of sales_order.jsp page
    var strShippingAddress="";

    // Added shipping address shipAddressWithoutContactPerson by veeru on 3rd June 2004 to reset the shippingAddressMap
    var shipAddressWithoutContactPerson = "";

    var tempStateZipCountry = "";
    if( contactPersonObj.value != "" )
    {
        strShippingAddress = strShippingAddress + contactPersonObj.value + "\n";
        //window.opener.addShippingAddressIdOption( contactPersonObj.value , -1 );
    }
    if ( address1Obj.value != "" )
    {
        strShippingAddress=strShippingAddress + address1Obj.value + "\n";
        shipAddressWithoutContactPerson = shipAddressWithoutContactPerson + address1Obj.value + "\n";
        window.opener.addShippingAddressIdOption( address1Obj.value , -1 );
    }
    if( address2Obj.value != "" )
    {
        strShippingAddress=strShippingAddress + address2Obj.value + "\n";
        shipAddressWithoutContactPerson = shipAddressWithoutContactPerson + address2Obj.value + "\n";
        window.opener.addShippingAddressIdOption( address2Obj.value , -1 );
    }
    if ( address3Obj.value != "" )
    {
        strShippingAddress=strShippingAddress+ address3Obj.value + "\n";
        shipAddressWithoutContactPerson = shipAddressWithoutContactPerson + address3Obj.value + "\n";
        window.opener.addShippingAddressIdOption( address3Obj.value , -1 );
    }
    if( address4Obj.value != "" )
    {
        strShippingAddress=strShippingAddress + address4Obj.value + "\n";
        shipAddressWithoutContactPerson = shipAddressWithoutContactPerson + address4Obj.value + "\n";
        window.opener.addShippingAddressIdOption( address4Obj.value , -1 );
    }
    if( cityObj.value != "" )
    {
        strShippingAddress=strShippingAddress + cityObj.value + "\n";
        shipAddressWithoutContactPerson = shipAddressWithoutContactPerson + cityObj.value + "\n";
        window.opener.addShippingAddressIdOption( cityObj.value , -1 );
    }
    if( stateObj.value != "" )
    {
        strShippingAddress = strShippingAddress + stateObj.value;
        shipAddressWithoutContactPerson = shipAddressWithoutContactPerson + stateObj.value;
        tempStateZipCountry = tempStateZipCountry + stateObj.value;
    }
    if( zipCodeObj.value != "" )
    {
        if( stateObj.value=="")
        {
            strShippingAddress=strShippingAddress + zipCodeObj.value;
            shipAddressWithoutContactPerson = shipAddressWithoutContactPerson + zipCodeObj.value;
            tempStateZipCountry=tempStateZipCountry + zipCodeObj.value;
        }
        else
        {
            strShippingAddress=strShippingAddress + " -" + zipCodeObj.value;
            shipAddressWithoutContactPerson = shipAddressWithoutContactPerson + " -" + zipCodeObj.value;
            tempStateZipCountry=tempStateZipCountry + " -" + zipCodeObj.value;
        }
    }
    if( countryObj.value != "" )
    {
        if( stateObj.value == "" && zipCodeObj.value == "" )
        {
            strShippingAddress = strShippingAddress + countryObj.value;
            shipAddressWithoutContactPerson = shipAddressWithoutContactPerson + countryObj.value;

            tempStateZipCountry = tempStateZipCountry + countryObj.value;
        }
        else
        {
            strShippingAddress = strShippingAddress + " ," + countryObj.value;
            shipAddressWithoutContactPerson = shipAddressWithoutContactPerson + " ,"  + countryObj.value;
            tempStateZipCountry = tempStateZipCountry + " ," + countryObj.value;
        }
    }

    if(tempStateZipCountry!="")
        window.opener.addShippingAddressIdOption( tempStateZipCountry , -1 );

    if( phoneNumberObj.value != "")
    {
        strShippingAddress  = strShippingAddress +"\n\nTel:"+ phoneNumberObj.value;
    }
    if( cellNumberObj.value != "")
    {
        strShippingAddress  = strShippingAddress +"\nCell:"+ cellNumberObj.value;
    }
    if( faxNumberObj.value != "")
    {
        strShippingAddress  = strShippingAddress +"\nFax:"+ faxNumberObj.value;
    }
    if( emailObj.value != "")
    {
        strShippingAddress  = strShippingAddress +"\nEmail:"+ emailObj.value;
    }

    shipAddressWithoutContactPerson  = shipAddressWithoutContactPerson +"\n\nTel:";
    shipAddressWithoutContactPerson  = shipAddressWithoutContactPerson +"\nCell:";
    shipAddressWithoutContactPerson  = shipAddressWithoutContactPerson +"\nFax:";
    shipAddressWithoutContactPerson  = shipAddressWithoutContactPerson +"\nEmail:";



    window.opener.addShippingAddressIdOption( "", -1 );

    //window.opener.addShippingAddressContactPerson( shipAddressWithoutContactPerson, strShippingAddress, theForm.contactPerson.value );

    //theForm.action.value = "SaveShippingAddress";
    //theForm.customerId.value = customerId;

    window.opener.document.packingSlipForm.shippingAddress.value = strShippingAddress;
    window.opener.document.packingSlipForm.allowNewShippingAddressEdit.value = "false";
    if( contactPersonObj.value != "" )
    {
        window.opener.document.packingSlipForm.contactPerson.value = contactPersonObj.value;
    }
    else
    {
       window.opener.document.packingSlipForm.contactPerson.value = "";
    }
    window.opener.document.packingSlipForm.newShippingAddressId.value=-1;
    window.opener.document.packingSlipForm['shippingAddressObject.contactPerson'].value = contactPersonObj.value;
    window.opener.document.packingSlipForm['shippingAddressObject.address1'].value = address1Obj.value;
    window.opener.document.packingSlipForm['shippingAddressObject.address2'].value = address2Obj.value;
    window.opener.document.packingSlipForm['shippingAddressObject.address3'].value = address3Obj.value;
    window.opener.document.packingSlipForm['shippingAddressObject.address4'].value = address4Obj.value;
    window.opener.document.packingSlipForm['shippingAddressObject.city'].value = cityObj.value;
    window.opener.document.packingSlipForm['shippingAddressObject.state'].value = stateObj.value;
    window.opener.document.packingSlipForm['shippingAddressObject.zipCode'].value = zipCodeObj.value;
    window.opener.document.packingSlipForm['shippingAddressObject.country'].value = countryObj.value;
    window.opener.document.packingSlipForm['shippingAddressObject.phoneNumber'].value = phoneNumberObj.value;
    window.opener.document.packingSlipForm['shippingAddressObject.faxNumber'].value = faxNumberObj.value;
    window.opener.document.packingSlipForm['shippingAddressObject.email'].value = emailObj.value;
    window.opener.document.packingSlipForm['shippingAddressObject.cellNumber'].value = cellNumberObj.value;
    document.packingSlipForm.submit();
    window.close();

}

function resetPage(theForm)
{
    theForm.reset();
}

function showErrorReportPage()
{
    newWindow = window.open ('<%=chipTracContext%>/reportProblem.do','reportProblem','channelmode=no,directories=no,toolbar=no,menubar=no,personalbar=no,status=no,scrollbars=no,width=750,height=400,resizable=no');
    newWindow.focus();
}
</SCRIPT>
</HEAD>
<BODY bgcolor="ffffff" marginheight="0" marginwidth="0" topmargin=0 leftmargin=0>
<%  String closeWindowAfterSessionTimeOut = ( String ) request.getAttribute( "closeWindowAfterSessionExp" );
    if ( closeWindowAfterSessionTimeOut == null )
    {
%>
<table cellpadding="10" cellspacing="0" border="0" width="100%">
<tr bgcolor="#8C87A8">
    <td height="30" valign="top" align=center><font class="NArialL">
        <font color="#FFFFFF">Add Shipping Address for - Customer</font><br>
    </td>
    <td valign="top" align="right">
        <a href="javascript:window.close()"><font class="NArial">Close</font></a>
    </td>
</tr>
</table>

<%--<html:form name="addressForm" action="/addAddress.do?action=EditShippingAddress" method="post" type="com.scalar.chiptrack.sales.forms.AddressForm" focus="address1">--%>
<html:form name="packingSlipForm" type="com.scalar.chiptrack.operations.packingslip.forms.PackingSlipForm" action="/packingSlip.do?action=SaveShippingAddress" scope="request">

<nested:nest property="newShippingAddress">
<center><span class="NArialW">Customer Shipping Address </span></center>
<img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height=10>
<b><FONT id="errors" color="FF0000" styleClass="NArialL"></FONT></b>
<br>
<TABLE border="1" width="100%" bordercolor="#FFFFFF">
 <TR>
       <TD width='40%' class="NArialL"> Contact Person </TD>
       <TD width='40%'><nested:text property="contactPerson" styleClass="NArialL" maxlength="50"/>
   </TR>
   <TR>
       <TD width='40%' class="NArialL"> Address 1 <font color="red">*</font></TD>
       <TD width='40%'><nested:text property="address1" styleClass="NArialL" maxlength="50"/>
   </TR>
   <TR>
       <TD width='40%' class="NArialL"> Address 2 </TD>
       <TD width='40%'><nested:text property="address2" styleClass="NArialL" maxlength="50"/>
   </TR>
    <TR>
       <TD width='40%' class="NArialL"> Address 3 </TD>
       <TD width='40%'><nested:text property="address3" styleClass="NArialL" maxlength="50"/>
   </TR>
   <TR>
       <TD width='40%' class="NArialL"> Address 4 </TD>
       <TD width='40%'><nested:text property="address4" styleClass="NArialL" maxlength="50"/>
   </TR>
   <TR>
       <TD width='40%' class="NArialL"> City </TD>
       <TD width='40%'><nested:text property="city" styleClass="NArialL" maxlength="50"/>
   </TR>
   <TR>
       <TD width='40%' class="NArialL"> State </TD>
       <TD width='40%'><nested:text property="state" styleClass="NArialL" maxlength="50"/>
   </TR>
   <TR>
       <TD width='40%' class="NArialL"> Zip Code </TD>
       <TD width='40%'><nested:text property="zipCode" styleClass="NArialL" maxlength="50"/>
   </TR>
    <TR>
       <TD width='40%' class="NArialL"> Country</TD>
       <TD width='40%'><nested:text property="country" styleClass="NArialL" maxlength="50"/>
   </TR>
    <TR>
       <TD width='40%' class="NArialL"> Phone Number</TD>
       <TD width='40%'><nested:text property="phoneNumber" styleClass="NArialL" maxlength="50" onkeypress='validatePhoneNumber(this)'/>
   </TR>
    <TR><!-- Added by Sarada on 8th March 2004  -->
       <TD width='40%' class="NArialL"> Cell No</TD>
       <TD width='40%'><nested:text property="cellNumber" styleClass="NArialL" maxlength="50" onkeypress='validatePhoneNumber(this)'/>
   </TR>
   <TR>
       <TD width='40%' class="NArialL"> Fax Number</TD>
       <TD width='40%'><nested:text property="faxNumber" styleClass="NArialL" maxlength="50" onkeypress='validatePhoneNumber(this)'/>
   </TR>
   <TR>
       <TD width='40%' class="NArialL"> Email </TD>
       <TD width='40%'><nested:text property="email" styleClass="NArialL" maxlength="50"/>
   </TR>
  
   <TR><TD colspan=2><img src="<%=chipTracContext%>/jsp/includes/images/spacer.gif" height='20'></TD></TR>
   <TR><TD colspan=2 align="center"><a href="javascript:addAddress();"><img src="<%=chipTracContext%>/jsp/includes/images/save.gif" border=0></a>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <a href="javascript:resetPage(document.packingSlipForm);"><img src="<%=chipTracContext%>/jsp/includes/images/reset.gif" border=0></a>
       </TD>
   </TR>
</table>

<input type="hidden" name="action" value="">
<input type="hidden" name="customerId" value="">

</nested:nest>

</html:form>
<% } else { %>
    <table cellpadding='0' cellspacing='0' bgcolor='FDF5E6' border="0" width="100%"  height="100%" align=center>
        <tr>
            <td>
                <table align="center" border="0" width="40%" bgcolor="#FDF5E6">
                    <tr><td>&nbsp;</td></tr>
                    <tr><td align="center" ><font class="NArialL" ><font color="red">Session has been time out , Please close this window and login again.</font></font></td></tr>
                    <tr><td>&nbsp;</td></tr>
                    <tr><td align="center" ><a href="javascript:window.close();"><font class='NArialL'>[Close this window]</font></a></td></tr>
                </table>
            </td>
       </tr>
       <tr><td>&nbsp;</td></tr>
    </table>
<% } %>
</HEAD>
</BODY>
</HTML>

